package cn.rulian.base.basis.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.ExampleMatcher.StringMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.rulian.base.basis.bo.CommonProperty;
import cn.rulian.base.basis.dao.CommonPropertyDao;
import cn.rulian.base.basis.dto.WeixinProperty;
import cn.rulian.base.basis.vo.CommonPropertyVo;
import cn.rulian.base.common.service.BaseService;
import cn.rulian.base.common.util.xx;
import cn.rulian.base.common.vo.Result;

/** 
 * 公共参数
 */
@Service
@Transactional(readOnly = true)
public class CommonPropertyService extends BaseService<CommonProperty>
{

	//private final Logger log = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private CommonPropertyDao dao; 

	@Override
	protected CommonPropertyDao getDao()
	{
		return dao;
	}
	
	//微信系统参数。作为缓存，提升性能
	private WeixinProperty weixinProperty = null;

	/**
	 * 查询
	 */
	public Result query(CommonPropertyVo vo)
	{
		Sort sort = new Sort(Direction.ASC, "code"); //倒序
		Pageable pageable = new PageRequest(vo.getPage()-1,vo.getRows(),sort); //分页查询条件
		
		String system = vo.getSystem();	//系统
		String group = vo.getGroup();	//分组
		String name = vo.getName();	//名称
		
		//创建查询条件数据对象
		CommonProperty tj = new CommonProperty();
		tj.setSystem(system);
		tj.setName(name);
		tj.setGroup(group);
		
		//创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写
   
        //创建实例
        Example<CommonProperty> ex = Example.of(tj, matcher); 
        
        //查询
        Page<CommonProperty> page = dao.findAll(ex,pageable);
        
		return Result.getResult(page);
	}
	
	/**
	 * 字符转换成数字
	 */
	private int convertInt(String value)
	{
		if(xx.isEmpty(value))
		{
			return 0;
		}
		try
		{
			int num = xx.toInt(value);
			return num;
		}
		catch (Exception e)
		{
			return 0;
		}
	}
	
	
	//更新微信参数
	public void updateWeixinProperty()
	{
		if(weixinProperty == null)
		{
			weixinProperty = new WeixinProperty();
		}
		List<CommonProperty> list = dao.findAll();
		for(CommonProperty pro:list)
		{
			if("11".equals(pro.getCode()) && "weixin".equals(pro.getSystem()))
			{//每天可发送投资消息（条数）
				weixinProperty.setMsgNumLimit(this.convertInt(pro.getValue()));
			}
			else if("12".equals(pro.getCode()) && "weixin".equals(pro.getSystem()))
			{//每天可发送交费提醒（条数）
				weixinProperty.setMsgNumLimit2(this.convertInt(pro.getValue()));
			}
			else if("13".equals(pro.getCode()) && "weixin".equals(pro.getSystem()))
			{//会员到期提醒（提前天数）
				weixinProperty.setBeforeDays(this.convertInt(pro.getValue()));
			}
		}
	}
	
	//返回微信参数
	public WeixinProperty getWeixinProperty()
	{
		if(weixinProperty == null)
		{
			this.updateWeixinProperty();
		}
		return weixinProperty;
	}
	
	
}
